package GUI;

import general.FileUtils;
import general.GlobalProperties;
import jsonObjects.Files;
import jsonObjects.Profile;
import org.apache.log4j.Logger;

import com.jgoodies.looks.plastic.Plastic3DLookAndFeel;

import serverHandling.HttpRequestHandlers;
import serverHandling.SessionObjects;

import javax.swing.*;

import java.io.File;

public class FileVersioningGUI extends JFrame {
    private static Logger log = Logger.getLogger(FileVersioningGUI.class);

    public FileVersioningGUI(File file, Profile profile) {
	this.profile = profile;
	this.file = file;
	initComponents();
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {
	try {
	    UIManager
		    .setLookAndFeel(new Plastic3DLookAndFeel());
	} catch (Exception e) {
	    log.error(e.getMessage());
	}
	Object[][] futureMenuItems;

	Files tmp = SessionObjects.getFileFromProfile(profile.getName(), file.getName());
	if (tmp != null) {
	    profileFileList = HttpRequestHandlers.versioning(tmp.getId());

	    if (profileFileList.length != 0) {
		futureMenuItems = new Object[profileFileList.length][profileFileList.length];
		for (int i = 0; i < profileFileList.length; i++) {
		    futureMenuItems[i][0] = profileFileList[i].getName();
		    futureMenuItems[i][1] = profileFileList[i].getCreated();
		}
	    } else
		futureMenuItems = new Object[0][0];
	} else
	    futureMenuItems = new Object[0][0];

	calncelButton = new JButton();
	okButton = new JButton();
	jScrollPane1 = new JScrollPane();
	jTable1 = new JTable();

	setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
	setForeground(java.awt.Color.white);
	setName("frame");

	calncelButton.setText("Cancel");
	calncelButton.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		jButton1ActionPerformed(evt);
	    }
	});

	okButton.setText("Ok");
	okButton.addMouseListener(new java.awt.event.MouseAdapter() {
	    public void mouseClicked(java.awt.event.MouseEvent evt) {
		jButton2MouseClicked(evt);
	    }
	});
	okButton.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		jButton2ActionPerformed(evt);
	    }
	});

	jTable1.setModel(new javax.swing.table.DefaultTableModel(
		futureMenuItems, new String[] { "Nazwa", "Wersja" }) {
	    Class[] types = new Class[] { String.class, String.class };
	    boolean[] canEdit = new boolean[] { false, true };

	    public Class getColumnClass(int columnIndex) {
		return types[columnIndex];
	    }

	    public boolean isCellEditable(int rowIndex, int columnIndex) {
		return canEdit[columnIndex];
	    }
	});
	jScrollPane1.setViewportView(jTable1);

	GroupLayout layout = new GroupLayout(getContentPane());
	getContentPane().setLayout(layout);
	layout.setHorizontalGroup(layout
		.createParallelGroup(GroupLayout.Alignment.LEADING)
		.addGroup(
			GroupLayout.Alignment.TRAILING,
			layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(
					layout.createParallelGroup(
						GroupLayout.Alignment.TRAILING)
						.addComponent(
							jScrollPane1,
							GroupLayout.DEFAULT_SIZE,
							380, Short.MAX_VALUE)
						.addGroup(
							layout.createSequentialGroup()
								.addGap(0,
									0,
									Short.MAX_VALUE)
								.addComponent(
									calncelButton)
								.addPreferredGap(
									LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(
									okButton)))
				.addContainerGap()));
	layout.setVerticalGroup(layout.createParallelGroup(
		GroupLayout.Alignment.LEADING).addGroup(
		GroupLayout.Alignment.TRAILING,
		layout.createSequentialGroup()
			.addContainerGap()
			.addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE,
				264, GroupLayout.PREFERRED_SIZE)
			.addPreferredGap(
				LayoutStyle.ComponentPlacement.RELATED,
				GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
			.addGroup(
				layout.createParallelGroup(
					GroupLayout.Alignment.BASELINE)
					.addComponent(calncelButton)
					.addComponent(okButton))
			.addContainerGap()));

	okButton.getAccessibleContext().setAccessibleName("okButton");

	pack();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

	this.setVisible(false);
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
	this.setVisible(false);
    }

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {

	if (jTable1.getSelectedRowCount() == 1) {

	    try {
		int row = jTable1.getSelectedRow();

		int column = jTable1.getSelectedColumn();
		String urObjctInCell = (String) jTable1.getValueAt(row,
			column - 1);
		Files fileToDownload = null;
		for (Files a : profileFileList)
		    if (a.getName().equals(urObjctInCell)) {
			fileToDownload = a;
			continue;
		    }

		if (fileToDownload != null) {
		    File toDelete = new File(GlobalProperties.getFolderMonit()
			    + profile.getName() + "\\" + file.getName());
		    FileUtils.deleteFileUnsafe(toDelete);
		    HttpRequestHandlers.download(
			    fileToDownload,
			    GlobalProperties.getFolderMonit()
				    + profile.getName() + "\\");
		}
	    } catch (Exception e) {
		log.error(e.getMessage());
	    }

	} else {
	    JOptionPane.showMessageDialog(null, "Nalezy wybrac profil!");
	}
	this.setVisible(false);
    }

    JButton calncelButton;
    JButton okButton;
    JScrollPane jScrollPane1;
    JTable jTable1;
    private File file;
    private Profile profile;

    private Files[] profileFileList;
}
